/**
 * add to project by meng.jy 2018-01-22
 */
var equipQRCode = {
	init : function() {
		// 初始化按钮
		setMenuButtonOpermission();
		systemType = getUrlParam("systemType");
		viewMode = {
			enterEquipList : ko.observable(),
			equipList : ko.observableArray(),
			equipNode : ko.observable(),
		}

		ko.applyBindings(viewMode);
		equipQRCode.evenBind();
	},
	evenBind : function() {
		// equipQRCode.loadDataGrid();
		// equipQRCode.getCheckType();
		$("#tree").treeview({
			speed : 500,
			showArea : true,
			showMenu : false,
			mulitCheck : false,
			showRights : true,
			showStation : true,
			showOrg : true, // 是否显示企业
			onNodeSelected : function(event, node) {

				equipQRCode.nodeSelected(node, equipQRCode.loadDataGrid);
			},
			onNodata : function() {
				$(".nolist").css("height", $("#list").outerHeight()).show();
				$("#list").hide();
			},
			resetSize : equipQRCode.resetSizePage,
			dataUrl : "/pls/lcp/qrcode/treeList"
		});

		$('#submit').bind('click', function() {

			var equipConfig = {};
			var eid = $("#hiddenEid").val();
			equipConfig.maintTypeList = [];
			$('#typeListDiv li').each(function(index, el) {
				if ($(this).hasClass('select')) {
					var maintType = {};

					if ($(this).attr('id') == "1") {

						maintType = {
							maintId : $(this).attr('id'),
							mainTypeName : $(this).attr('id'),
							eid : eid,
							rate : $("#carbonrate").val(),
							day : $("#carbonday").val()
						};

					} else if ($(this).attr('id') == "2") {

						maintType = {
							maintId : $(this).attr('id'),
							mainTypeName : $(this).attr('id'),
							eid : eid,
							rate : $("#potionrate").val(),
							day : $("#potionday").val()
						};

					} else if ($(this).attr('id') == "3") {

						maintType = {
							maintId : $(this).attr('id'),
							mainTypeName : $(this).attr('id'),
							eid : eid,
							rate : $("#dailypatrolrate").val(),
							day : $("#dailypatrolday").val()
						};

					} else if ($(this).attr('id') == "4") {

						maintType = {
							maintId : $(this).attr('id'),
							mainTypeName : $(this).attr('id'),
							eid : eid,
							rate : "",
							day : ""
						};

					} else if ($(this).attr('id') == "5") {

						maintType = {
							maintId : $(this).attr('id'),
							mainTypeName : $(this).attr('id'),
							eid : eid,
							rate : $("#filternetrate").val(),
							day : $("#filternetrate").val()
						};
					}
					equipConfig.maintTypeList.push(maintType);

				}
			});
			if (equipConfig.maintTypeList.length === 0) {
				ShowMessage('请选择维护类型!', 'warning');
				return false;
			}

			$('#equipMaintForm').form('submit', {
				url : getProjectUrl('/gems/equipqrcode/edit'),
				onSubmit : function(param) {

					equipConfig.eid = eid;
					param.equipConfig = JSON.stringify(equipConfig);
				},

				success : function(data) {
					window.top.$("#frameMain")[0].contentWindow.equipQRCode.loadDataGrid();
					equipQRCode.closeWindow();
				}

			});

		});
		// 取消
		$("#cancel").bind("click", function() {

			$("#equipCheckDeployForm").form("clear");
			$('#solveDiv').window('close');
		});

		$("#closeAll").bind("click", function() {

			$('#equipQR').window('close');

		});

	},
	loadDataGrid : function(param) {
		$("#list").show();
		$(".nolist").hide();

		var result;
		ajaxProcess({
			url : getProjectUrl('/lcp/qrcode/dischargePoints'),
			requestType : 'POST',
			async : false,
			data : param,
			successHandle : function(data) {
				result = data.rows;
				if (data.rows === null || data.rows.length === 0) {
					$("#list").hide();
					$(".nolist").css("height", $("#list").outerHeight()).show();
				}
				if (result) {
					var enterEquipList = {};
					enterEquipList.enterID = result[0].psCode;
					enterEquipList.enterName = result[0].enterName;
					enterEquipList.equipList = result;
					viewMode.enterEquipList(enterEquipList);
				}
			}
		});

		return result;
	},
	nodeSelected : function(node, callBack) {
		var param = {};
		var sid = node[0].id;
		var type = node[0].type;
		var name = node[0].text;
		if (type === "enter") {
			param.psCode = sid;
		}
		if (param.psCode) {
			callBack(param);
		}
	},
	closeWindows : function() {

		$('#qrWindow').window('close');
	},

	// 关闭窗口
	closeWindow : function() {

		$('#solveDiv').window('close');
	},
	openMonitor : function(data) {
		var div = document.getElementById('erWindow');
		var height = (document.body.clientHeight - $("#erWindow").height()) * 0.1;
		$(".window").css("top", height);
		$('#erWindow').window('open');
		var myChart = echarts.init(document.getElementById("window-echarts"));

		ajaxProcess({
			url : getProjectUrl('/app/monitor/autoMonitorline'),
			requestType : 'POST',
			async : false,
			data : {
				psCode : null,
				pointCode : null,
				totalFlags : 'dataValue'
			},
			successHandle : function(data) {
				var option = data.data;
				option.yAxis = {
					type : 'value'
				};
				option.xAxis.boundaryGap: false,
				option.tooltip = {
					trigger : 'axis',
					formatter: function (params) {
						debugger;
					    return '{a}<br />{b}: {c}<br />{b}: {c}';
					}
				};
				myChart.setOption(option);
			}
		});
	},
	loadQRCode : function(data) {
		var div = document.getElementById('qrWindow');
		var height = (document.body.clientHeight - $("#qrWindow").height()) * 0.25;
		$(".window").css("top", height);
		$('#qrWindow').window('open');

		viewMode.equipNode(data);
		$('#showEidQR').html("");
		var qrcode = new QRCode(document.getElementById("showEidQR"), {
			width : 130,// 设置宽高
			height : 130,
			correctLevel : QRCode.CorrectLevel.H
		});
		var qrCode = {};
		qrCode.pointCode = data.pointCode;
		qrCode.psCode = data.psCode;
		qrCode.enterName = data.enterName;
		qrCode.codeControlLevel = data.codeControlLevel;
		qrCode.createTime = data.createTime;
		$("#showEidQR").attr("QRName", qrCode.enterName + "[" + data.pointCode + "]");
		qrcode.clear();
		qrcode.makeCode(JSON.stringify(qrCode));
	},

	loadMultQRCodes : function(data) {

		var div = document.getElementById('equipQR');
		var height = (document.body.clientHeight - $("#equipQR").height()) * 0.25;
		$(".window").css("top", height);
		$('#equipQR').window('open');

		var equipList = data.equipList;
		viewMode.equipList(equipList);
		if (equipList) {
			for (var i = 0; i < equipList.length; i++) {
				$("#" + equipList[i].pointCode).html("");
				if (!isEmpty(equipList[i].pointCode)) {
					var qrcode = new QRCode(document.getElementById(equipList[i].pointCode), {
						width : 130,// 设置宽高
						height : 130,
						correctLevel : QRCode.CorrectLevel.H
					});
					var qrCodeData = {};
					qrCodeData.pointCode = equipList[i].pointCode;
					qrCodeData.psCode = equipList[i].psCode;
					qrCodeData.enterName = equipList[i].enterName;
					qrCodeData.codeControlLevel = equipList[i].codeControlLevel;
					qrCodeData.createTime = equipList[i].createTime;
					$("#" + equipList[i].pointCode).attr("QRName",
							qrCodeData.enterName + "[" + equipList[i].pointCode + "]");
					qrcode.clear();
					qrcode.makeCode(JSON.stringify(qrCodeData));
				}
			}
		}
	},

	exportSingel : function() {
		$("#showEidQR").find("canvas").each(function(index, canvas) {
			var name = $(canvas).parent().attr("QRName") + ".png";
			exportCanvasAsPNG(canvas, name);
		});
	},

	exportMulti : function() {
		$("#equipQR").find("canvas").each(function(index, canvas) {
			var name = $(canvas).parent().attr("QRName") + ".png";
			exportCanvasAsPNG(canvas, name);
		});
	}
};

$(document).ready(function() {
	resetSize();
	equipQRCode.init();
});

function exportCanvasAsPNG(canvas, fileName) {
	var MIME_TYPE = "image/png";
	var dlLink = document.createElement('a');
	dlLink.download = fileName;
	dlLink.href = canvas.toDataURL("image/png");
	dlLink.dataset.downloadurl = [ MIME_TYPE, dlLink.href ].join(':');
	document.body.appendChild(dlLink);
	dlLink.click();
	document.body.removeChild(dlLink);
}